package ext;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.jar.JarUnArchiver;

/**
 * 
 * @author 北京八维博大科技 薛建新
 * @date 2022年11月10日 13:58:05
 * @Copyright 北京八维博大科技
 */
public class CryptoJar {

	public static void main(String[] args) {
//		try {
//			File f=new File(new URL("file:/D:/a/BOOT-INF/lib1/demo20220728test-1.0.0.jar").toURI());
//			System.out.println(f.exists());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		doCryptoJar();
	}

	private static void doCryptoJar() {
		//
		String basePath = "D:/work20220906/java/myopen/springboot2/demo20220728/target/";
		String jarName = "demo20220728-0.0.1-SNAPSHOT.jar";
		try {

			// 解压目录
			File destDirectory = new File("d:/a/");
			if (destDirectory.exists()) {
				System.out.println(destDirectory.delete());
			}
			destDirectory.mkdir();
			// 1. 解压jar包
			// 参考
			// 解压
			// http://www.javased.com/?api=org.codehaus.plexus.archiver.UnArchiver
			// 打包
			// https://www.tabnine.com/code/java/classes/org.codehaus.plexus.archiver.jar.JarArchiver
			JarUnArchiver jarUnArchiver = new JarUnArchiver();
			jarUnArchiver.setDestDirectory(destDirectory);// 目录必须存在
			jarUnArchiver.setSourceFile(new File(basePath, jarName));// 要解压的文件
			// IncludeExcludeFileSelector includeExcludeFileSelector=new
			// IncludeExcludeFileSelector();
			// includeExcludeFileSelector.setExcludes(excludes);
			// includeExcludeFileSelector.setIncludes(includes);
			// jarUnArchiver.setFileSelectors(new FileSelector[]{includeExcludeFileSelector});
			jarUnArchiver.extract();

			// 2.修改解压文件
			//替换loader
			String loaderPath="D:/work20220906/java/myopen/springboot2/demo20220728/target/classes/org/springframework/boot/loader";
			
			
			Files.walk(Path.of(loaderPath),1)
//			.peek(p-> System.out.println(p.getFileName())) 
			.filter(p->p.getFileName().toString().startsWith("JarLauncher"))
//			.peek(p-> System.out.println("-->"+p)) 
			.forEach(p->{
				
				try {
					Files.copy(p, Path.of(destDirectory.toString(),"org/springframework/boot/loader",p.getFileName().toString()),StandardCopyOption.REPLACE_EXISTING);
				} catch (IOException e) {
					e.printStackTrace();
				}
			});
			
			//加密jar并拷贝到lib1 目录必须存在
			//Files.copy(Paths.get("D:/work20220906/java/myopen/springboot2/demo20220728test/target", "demo20220728test-1.0.0.out"), Paths.get(destDirectory.toString(), "/BOOT-INF/lib1/demo20220728test-1.0.0.jar"),StandardCopyOption.REPLACE_EXISTING);
			Files.copy(Paths.get("D:/work20220906/java/myopen/springboot2/demo20220728test/target", "demo20220728test-1.0.0.jar"), Paths.get(destDirectory.toString(), "/BOOT-INF/lib/demo20220728test-1.0.0.jar"),StandardCopyOption.REPLACE_EXISTING);

			// 3.重新打包
			/**/
			JarArchiver jarArchiver = new JarArchiver();
			jarArchiver.setDestFile(new File("d:/a.jar"));
			jarArchiver.addDirectory(destDirectory);
			jarArchiver.setCompress(false);// 重新打包不能压缩
			jarArchiver.setManifest(new File(destDirectory, "META-INF/MANIFEST.MF"));// 必须重新添加这个文件
			jarArchiver.createArchive();
			

			System.out.println("========done========");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
